//
//  PlayView.m
//  27-Block练习-播放按钮
//
//  Created by 曹曹 on 16/8/19.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
#import "GX_Button.h"

@interface PlayView ()
@property (nonatomic, strong) GX_Button *playButton;
@property (nonatomic, strong) GX_Button *nextButton;
@property (nonatomic, strong) GX_Button *lastButton;
@property (nonatomic, copy) PlayButton play;
@property (nonatomic, copy) PauseButton pause;
@property (nonatomic, copy) LastButton last;
@property (nonatomic, copy) NextButton next;
@end

@implementation PlayView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}

// 注意这个方法在初始化方法里, 属性使用下划线形式
- (void)setUp {
    _playButton = [self buttonWithImageName:@"btn_pause_hl" action:@selector(play:)];
    _nextButton = [self buttonWithImageName:@"nextButton" action:@selector(next:)];
    _lastButton = [self buttonWithImageName:@"lastButton" action:@selector(last:)];
}

- (void)play:(PlayButton)play pause:(PauseButton)pause {
    self.play = play;
    self.pause = pause;
}

- (void)last:(LastButton)last next:(NextButton)next {
    self.last = last;
    self.next = next;
}

// 点击播放按钮后, 移除播放的点击事件, 添加暂停的点击事件
- (void)play:(GX_Button *)playButton {
    [playButton setImage:[UIImage imageNamed:@"btn_play_hl"] forState:UIControlStateNormal];
    [playButton removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [playButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    self.play(playButton);
}

- (void)pause:(GX_Button *)pauseButton {
    [pauseButton setImage:[UIImage imageNamed:@"btn_pause_hl"] forState:UIControlStateNormal];
    [pauseButton removeTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
    [pauseButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    self.pause(pauseButton);
}


- (void)last:(GX_Button *)lastButton {
    self.last(lastButton);
}

- (void)next:(GX_Button *)nextButton {
    self.next(nextButton);
}

- (GX_Button *)buttonWithImageName:(NSString *)imageName action:(SEL)action {
    GX_Button *button = [GX_Button buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    return button;
}

// 在这个方法中写frame
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat playButtonW = CGRectGetHeight(self.bounds) - 2;
    CGFloat lastButtonW = CGRectGetHeight(self.bounds) * 4 / 6.0;
    _playButton.frame = CGRectMake(0, 0, playButtonW, playButtonW);
    // 注意: 不要直接等于self.center, self.center相对于自身父视图, 直接等于的话, button的中心点会按照self父视图的宽和高去计算
    // _playButton.center = self.center
    _playButton.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
    // 上一首 下一首 宽度
    _lastButton.gx_width = lastButtonW;
    _lastButton.gx_height = lastButtonW;
    _lastButton.gx_centerY = _playButton.gx_centerY;
    _lastButton.gx_centerX = _playButton.gx_x / 2;
    _nextButton.gx_width = lastButtonW;
    _nextButton.gx_height = lastButtonW;
    _nextButton.gx_centerY = _playButton.gx_centerY;
    _nextButton.gx_centerX = _playButton.gx_width + _playButton.gx_x * 3 / 2;
}

@end
